define([
    "UtilDir/util",
    "UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
    "Date", "DateCN", "css!DateCss",
    "EasyUI", "EasyUI-lang"
], function (Util, Tool, SelectOrg, SearchBlock) {

    var getController = function () {
        return getServer() + "/pc";
    };
    /**
     * 页面初始化
     */
    var init = function () {
        createGrid();

    };

    var crucialCountys = ['内蒙古自治区/锡林郭勒盟/西乌珠穆沁旗','黑龙江省/伊春市/丰林县','江西省/赣州市/大余县','山东省/滨州市/博兴县','河南省/三门峡市/灵宝市','广西壮族自治区/防城港市/东兴市','四川省/成都市/金堂县','贵州省/遵义市/播州区','贵州省/黔南布依族苗族自治州/福泉市','陕西省/西安市/灞桥区','甘肃省/甘南藏族自治州/舟曲县']
    var createGrid = function () {
        //percentage中间库 quantity业务库
        $.ajax({
            url : getServer()+"/pc/yhSchedule/find3",
            type:'get',
            success:function (data) {
                // 最后一行加合计
                var arr = [{"quantity":0,"percentage":0},{"quantity":0,"percentage":0},{"quantity":0,"percentage":0},{"quantity":0,"percentage":0},{"quantity":0,"percentage":0},{"quantity":0,"percentage":0},{"quantity":0,"percentage":0}];
                for (var i=0;i<data.length;i++){
                    for(var j=0;j<7;j++){
                        if(data[i].dataVOS[j].quantity != null)
                            arr[j].quantity+=parseInt(data[i].dataVOS[j].quantity);
                        if(data[i].dataVOS[j].percentage != null)
                            arr[j].percentage+=parseInt(data[i].dataVOS[j].percentage);
                    }
                }
                var obj = {
                    "divisionName":"合计",
                    "dataVOS":arr
                }
                data.push(obj); //obj是最后一行

                loadGrid(data);
            }
        });
    }

    var loadGrid = function (data) {
        $("#zdyhMiddleDataGrid").datagrid({
            // url: getServer()+"/pc/yhSchedule/find1",
            // method: "GET",
            data:data,
            fitColumns: true,
            autoRowHeight: false,
            nowrap: false,
            //nowrap:false,
//          fit:true,
//          striped: true,
            frozenColumns: [[
                {
                    field: 'divisionName',
                    title: '省份',
                    width: '12%',
                    align: 'center',
                    styler:function(val,rowData,rowIndex){
                        if (crucialCountys.indexOf(rowData.divisionName)!= -1)
                            return 'background:#e4e9fc';
                    }
                }
            ]],
            columns: [
                [
                    {
                        title:'城镇住宅',
                        colspan: 2,
                        align: 'center',
                    },
                    {

                        title: '农村独立住宅',
                        colspan: 2,
                        align: 'center',
                    },
                    {
                        title: '农村集合住宅',
                        colspan: 2,
                        align: 'center',
                    },
                    {
                        title: '城镇非住宅',
                        colspan: 2,
                        align: 'center',
                    },
                    {
                        title: '农村非住宅',
                        colspan: 2,
                        align: 'center',
                    },
                    {
                        title: '市政桥梁',
                        colspan: 2,
                        align: 'center',
                    },
                    {
                        title: '供水厂',
                        colspan: 2,
                        align: 'center',
                    },
                    {
                        title: '合计',
                        colspan: 2,
                        align: 'center',
                    },
                ],
                [
                    {
                        field:'cityHouse1',
                        title: '中间库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[0].percentage == null)
                                return 0;
                            else
                                return rowData.dataVOS[0].percentage;
                        }
                    },
                    {
                        field:'cityHouse2',
                        title: '业务库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[0].quantity == null)
                                return 0;
                            else
                                return rowData.dataVOS[0].quantity;
                        }
                    },
                    {
                        field:'countrysideIndependent1',
                        title: '中间库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[3].percentage == null)
                                return 0;
                            else
                                return rowData.dataVOS[3].percentage;
                        }
                    },
                    {
                        field:'countrysideIndependent2',
                        title: '业务库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[3].quantity == null)
                                return 0;
                            else
                                return rowData.dataVOS[3].quantity;
                        }
                    },
                    {
                        field:'countrysideCollective1',
                        title: '中间库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[2].percentage == null)
                                return 0;
                            else
                                return rowData.dataVOS[2].percentage;
                        }
                    },
                    {
                        field:'countrysideCollective2',
                        title: '业务库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[2].quantity == null)
                                return 0;
                            else
                                return rowData.dataVOS[2].quantity;
                        }
                    },
                    {
                        field:'cityNotHouse1',
                        title: '中间库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[1].percentage == null)
                                return 0;
                            else
                                return rowData.dataVOS[1].percentage;
                        }
                    },
                    {
                        field:'cityNotHouse2',
                        title: '业务库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[1].quantity == null)
                                return 0;
                            else
                                return rowData.dataVOS[1].quantity;
                        }
                    },
                    {
                        field:'countrysideNotHouse1',
                        title: '中间库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[4].percentage == null)
                                return 0;
                            else
                                return rowData.dataVOS[4].percentage;
                        }
                    },
                    {
                        field:'countrysideNotHouse2',
                        title: '业务库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[4].quantity == null)
                                return 0;
                            else
                                return rowData.dataVOS[4].quantity;
                        }
                    },
                    {
                        field:'govBridge1',
                        title: '中间库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[5].percentage == null)
                                return 0;
                            else
                                return rowData.dataVOS[5].percentage;
                        }
                    },
                    {
                        field:'govBridge2',
                        title: '业务库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[5].quantity == null)
                                return 0;
                            else
                                return rowData.dataVOS[5].quantity;
                        }
                    },
                    {
                        field:'waterWorks1',
                        title: '中间库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[6].percentage == null)
                                return 0;
                            else
                                return rowData.dataVOS[6].percentage;
                        }
                    },
                    {
                        field:'waterWorks2',
                        title: '业务库',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            if(rowData.dataVOS[6].quantity == null)
                                return 0;
                            else
                                return rowData.dataVOS[6].quantity;
                        }
                    },
                    {
                        field:'middleSum',
                        title: '中间库合计',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            var percentageSum = 0;
                            for(var i=0;i<rowData.dataVOS.length;i++){
                                if (rowData.dataVOS[i].percentage != null)
                                    percentageSum += parseInt(rowData.dataVOS[i].percentage)
                            }
                            return percentageSum;
                        }
                    },
                    {
                        field:'workSum',
                        title: '业务库合计',
                        align: 'center',
                        width:'10%',
                        formatter:function(val,rowData,rowIndex){
                            var quantitySum = 0;
                            for(var i=0;i<rowData.dataVOS.length;i++){
                                if (rowData.dataVOS[i].quantity != null)
                                    quantitySum += parseInt(rowData.dataVOS[i].quantity)
                            }
                            return quantitySum;
                        }
                    },
                ],
            ],
            toolbar: [{
                iconCls: 'fa fa-download',
                text: "导出",
                handler: function () {
                    exportForm();
                }
            }],
        });
    };
    // 导出
    var exportForm = function () {
        window.location.href = getServer() + "/pc/yhSchedule/export3";
    }



    return {
        init: init
    };
});
